<?php
class CacheGlobal{
    private static $cacher = null;
    protected $data=[];
    public static function new(){
        if(self::$cacher == null){self::$cacher = new CacheGlobal();}
        return self::$cacher;
    }
    public static function newc(){
        if(self::$cacher == null){self::$cacher = new CacheGlobal();}
        return self::$cacher;
    }
    public function get($name=null,$def=null){
        if(!$name){
            return $this->data;
        }
        $names=explode('.',$name);
        $value=&$this->data;
        foreach ($names as $n){
            if(!$n){
                continue;
            }
            if(isset($value[$n])){
                $value=&$value[$n];
            }else{
                return $def;
            }
        }
        return $value;
    }

    public function set($name, $data,$merge=null){
        if(!$name){
            return false;
        }
        $names=explode('.',$name);
        $value=&$this->data;
        foreach ($names as $n){
            if(isset($this->data[$n])){
                $value=&$this->data[$n];
            }else{
                //赋值
                $value[$n]=[];
                $value=&$value[$n];
            }
        }
        //赋值
        if($merge==true){
            if(is_array($data) && is_array($value)){
                $value=array_merge($value,$data);
                return true;
            }
            return false;
        }
        $value=$data;
        return true;
    }

    public function remove($name){
        if(!$name){
            $this->clear();
        }
        $names=explode('.',$name);
        $value=&$user_config;
        $match=false;
        foreach ($names as $n){
            if(isset($this->data[$n])){
                $value=&$this->data[$n];
                $match=true;
            }else{
                break;
            }
        }
        if($match){
            unset($value);
        }
        return true;
    }

    public function clear(){
        $this->data=[];
        return true;
    }

    public function close(){

    }
//    public function __debugInfo()
//    {
//        return json_decode(json_encode($this), true);
//    }
}